梦入琼楼寒有月,行过石树冻无烟

Laravel 数据库

如果我们要开发一个交互性非常强的站点,那么数据库交互就是这其中必不可少的一项,在目前,Laravel主要支持四种主流的数据库分别为:“MySQL、PostgreSQL、SQLite、SQL Server”。

配置

在Laravel之中,配置数据库主要可以通过config/database.php类进行,也可通过配置.env文件来实现,本文我们以配置MySQL为例:

database.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'toor'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

.env

1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=toor

CURD (增删改查)


在实际的开发项目过程中,对数据库操作最为主要的就是“CURD”即增删改查的一系列操作,对此Laravel为我们提供了一个专门用于进行CURD操作的控制器模板,我们可以通过使用命令进行创建:

php artisan make:controller PostController –resource

之后创建的数据库CURD模板将会自动在app/Http/Controllers目录下生成,至控制器的信息我们可以重新回顾下6.Laravel 控制器所学的知识。

插入数据

在Laravel之中,我们可以引入DB扩展中的insert方法来进行插入,且支持多数据插入如下:

input.blode.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<title>This is be Input up</title>
</head>
<body>
<form action="/input/test" method="post">
@csrf
<input type="hidden" name="_token" value="<?php echo csrf_token()?>">
Username:<input type="text" name="id" />&nbsp;
<input type="submit" value="up">
</form>
</body>
</html>

PostController

1
2
3
4
5
6
7
8
9
10
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
{
$id = $request->input('id');
DB::insert('insert into user(id) value(?)', [$id]);
}

web.app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
use Illuminate\Http\Request;

Route::get('/', function () {
return view('welcome');
});

Route::get('/input', function () {
return view(('input'));
});

Route::post('/input/test', 'PostController@create');

删除数据


在增删改查中,删除数据排第二,也是一项非常常用的需求,通常只有管理员权限才可以做到这种功能,如果使用Laravel来实现的话需要delete方法:

PostController

1
2
3
4
5
6
7
8
9
10
11
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request)
{
$id = $request->input('id');
DB::delete('delete from user where id = ?',[$id]);
}

input.blode.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<title>This is be Input up</title>
</head>
<body>
<form action="/input/test" method="post">
@csrf
<input type="hidden" name="_token" value="<?php echo csrf_token()?>">
Username:<input type="text" name="id" />&nbsp;
<input type="submit" value="up">
</form>
</body>
</html>

web.php

1
2
3
Route::get('/show', 'PostController@show');

Route::post('/input/test', 'PostController@destroy');

修改数据


在本次的演示当中,我们主要将数据库中的2更改为20,当然由于我们数据库中只有一个”id”字段,如果需要作出指定修改你需要建立两个字段,来进行配合,本次我们只演示如何修改字段数据,可以使用update数据:

PostController

1
2
3
4
5
6
7
8
9
10
11
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Request $request)
{
$id = $request->input('id');
DB::update('update user set id=?',[$id]);
}

input.blude.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<title>This is be Input up</title>
</head>
<body>
<form action="/input/test" method="post">
@csrf
<input type="hidden" name="_token" value="<?php echo csrf_token()?>">
Username:<input type="text" name="id" />&nbsp;
<input type="submit" value="up">
</form>
</body>
</html>

web.php

1
2
3
4
5
Route::get('/input', function () {
return view(('input'));
});

Route::post('/input/test', 'PostController@edit');

查询数据


对于Laravel的数据查询,也是一项非常简单且优雅的语法形式,其主要使用select方法进行查询:

PostControoller

1
2
3
4
5
6
7
8
9
10
11
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show()
{
$users = DB::select('select * from user');
return view('input', ['users'=>$users]);
}

input.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<head>
<title>This is be Input up</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{$user->id}}</td>
</tr>
@endforeach
</table>
</body>
</html>

web.php

1
Route::get('/show', 'PostController@show');
⬅️ Go back